<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved. 
 * feature summary.
 * @filesource  RansomCommand.php 
 * @author      lipeng <lipengage@gmain.com> 
 * @package     / 
 * @version     $Id: $ 
 */

class RansomCommand extends command{
	public function excute() {
		$userdb = new user_dao();
		$activitydb = new activity_dao();

		$viewerid = $this->request['vid'];
		$container = $this->request['ctn'];
		
		if (empty($viewerid) || empty($container)) {
			$this->set_messageid(command::$PARAM_ERROR);
			return true;
		}
		
		$viewer = $userdb->user_get_byid($container, $viewerid);
		if ($viewer["count"] < 1) {
			$this->set_messageid(command::$PARAM_ERROR);
			return true;
		}
		else {
			$viewer = $viewer["result"][0];
		}
		
		if (empty($viewer["master_id"])) {
			$this->set_messageid(command::$DONT_HAVE_MASTER);
			return true;
		}

		$master = $userdb->user_get_byid($container, $viewer["master_id"]);
		if ($master["count"] < 1) {
			$this->set_messageid(command::$PARAM_ERROR);
			return true;
		}
		else {
			$master = $master["result"][0];
		}
		
		if ($viewer["cash"] < $viewer["price"]) {
			$this->set_messageid(command::$NOT_ENOUGH_MONEY);
			return true;
		}
		
		$this->set_haserror(false);
		$params["viewer"] = $viewer;
		$params["master"] = $master;

		$viewer["cash"] -= $viewer["price"];
		$viewer["master_id"] = "";
		$viewer["master_name"] = "";
		$master["cash"] += $viewer["price"];
		$master["slave_num"] -= 1;
		$userdb->user_update_byid($container, $viewer["uid"], $viewer);
		$userdb->user_update_byid($container, $master["uid"], $master);

		$new_activity = array();
		$new_activity["container"] = $viewer["container"];
		$new_activity["command"] = command::$RANSOM;
		$new_activity["time"] = time();
		$new_activity["actor"] = $viewer["uid"];
		$new_activity["actor_name"] = $viewer["name"];
		$new_activity["target"] = $master["uid"];
		$new_activity["target_name"] = $master["name"];
		$new_activity["type"] = activity_dao::$BUY;
		$activitydb->activity_insert($new_activity);
		$this->set_messageid(command::$RANSOM);
		$this->set_params($params);
		return true;
	}
}
?>